﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Manage : MonoBehaviour
{
    public static Manage instance;
    const int key_pergroup_num = 7;
    const int key_group_num = 7;

    public GameObject key_prefab;
    public GameObject real_layout;

    public GameObject layout1;
    public GameObject layout2;
    public GameObject layout3;
    public GameObject layout4;
    public MusicText Scrolltext;

    public GameObject musicalNote_prefab;

    public AudioClip[] whiteSounds1 = new AudioClip[7];
    public AudioClip[] whiteSounds2 = new AudioClip[7];
    public AudioClip[] whiteSounds3 = new AudioClip[7];
    public AudioClip[] whiteSounds4 = new AudioClip[7];
    public AudioClip[] whiteSounds5 = new AudioClip[7];
    public AudioClip[] whiteSounds6 = new AudioClip[7];
    public AudioClip[] whiteSounds7 = new AudioClip[7];

    public int old_index = 3;

    Layout layout;
    Vector2 key_size;
    Key[,] keys = new Key[key_group_num, key_pergroup_num];//所有按键
    Transform[] positons = new Transform[7];//布局
    GameObject cur_layout;
    private void Awake()
    {
        instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        SetLayout(GameData.layoutIndex);

        switch (GameData.musicIndex)
        {
            case 1:
                Scrolltext.SetMusic(Musices.music1);
                break;
            case 2:
                Scrolltext.SetMusic(Musices.music2);
                break;
            case 3:
                Scrolltext.SetMusic(Musices.music3);
                break;
            case 4:
                Scrolltext.SetMusic(Musices.music4);
                break;
            case 5:
                Scrolltext.SetMusic(Musices.music5);
                break;
            case 6:
                Scrolltext.SetMusic(Musices.music6);
                break;
            case 7:
                Scrolltext.SetMusic(Musices.music7);
                break;
            case 8:
                Scrolltext.SetMusic(Musices.music8);
                break;
            default:
//                Scrolltext.SetMusic(Musices.all_music[GameData.musicIndex - 8]);
                break;
        }

        
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            SceneManager.LoadScene(0);
        }
    }

    public void SetSoundActive(int index)
    {
        for (int i = 0; i < keys.GetLength(1); i++)
        {
            keys[old_index, i].gameObject.SetActive(false);
        }
        for (int i = 0; i < keys.GetLength(1); i++)
        {
            keys[index, i].gameObject.SetActive(true);
        }
        
        old_index = index;
    }

    public void SetSoundActive_2(int index)
    {
        for (int i = 0; i < layout.keys.Length; i++)
        {
            layout.keys[i].SetClip(GetAuidoClips(index)[i]);
        }
    }

    AudioClip[] GetAuidoClips(int index)
    {
        AudioClip[] audioClips;
        switch (index)
        {
            case 0:
                audioClips = whiteSounds1;
                break;
            case 1:
                audioClips = whiteSounds2;
                break;
            case 2:
                audioClips = whiteSounds3;
                break;
            case 3:
                audioClips = whiteSounds4;
                break;
            case 4:
                audioClips = whiteSounds5;
                break;
            case 5:
                audioClips = whiteSounds6;
                break;
            case 6:
                audioClips = whiteSounds7;
                break;
            default:
                audioClips = whiteSounds1;
                break;
        }
        return audioClips;
    }

    //固定格式的Gameobject
    public Transform[] GetLayout(GameObject layout)
    {
        Transform[] _transform = new Transform[7];
        _transform[0] = layout.transform.Find("w1");
        _transform[1] = layout.transform.Find("w2");
        _transform[2] = layout.transform.Find("w3");
        _transform[3] = layout.transform.Find("w4");
        _transform[4] = layout.transform.Find("w5");
        _transform[5] = layout.transform.Find("w6");
        _transform[6] = layout.transform.Find("w7");
        return _transform;
    }

    public void SetLayout(int index)
    {
        switch (index)
        {
            case 0:
                SetLayout(layout1);
                break;
            case 1:
                SetLayout(layout2);
                break;
            case 2:
                SetLayout(layout3);
                break;
            case 3:
                SetLayout(layout4);
                break;
            default:
                SetLayout(layout1);
                break;
        }
    }

    void SetLayout(GameObject layout)
    {
        if (cur_layout != null)
        {
            cur_layout.SetActive(false);
        }
        cur_layout = layout;
        cur_layout.SetActive(true);
        positons = this.GetLayout(cur_layout);
        UpdateLayout();
    }

    void UpdateLayout()
    {
        for (int i = 0; i < key_group_num; i++)
        {
            for (int j = 0; j < key_pergroup_num; j++)
            {
                GameObject key_real = Object.Instantiate(key_prefab);
                
                key_real.transform.SetParent(real_layout.transform);
                float k = Screen.height / 1080f;
                float width = positons[j].GetComponent<RectTransform>().rect.width;
                float height = positons[j].GetComponent<RectTransform>().rect.height;
                key_real.GetComponent<RectTransform>().sizeDelta = new Vector2(k * width, k * height);
                key_real.GetComponent<RectTransform>().anchoredPosition = positons[j].GetComponent<RectTransform>().anchoredPosition;
                key_real.GetComponent<RectTransform>().anchorMin = positons[j].GetComponent<RectTransform>().anchorMin;
                key_real.GetComponent<RectTransform>().anchorMax = positons[j].GetComponent<RectTransform>().anchorMax;
                key_real.GetComponent<RectTransform>().localPosition = positons[j].localPosition;                
                key_real.GetComponent<RectTransform>().localScale = positons[j].localScale;                
                key_real.GetComponent<Image>().sprite = positons[j].GetComponent<Image>().sprite;
                key_real.GetComponent<Image>().type = positons[j].GetComponent<Image>().type;
                key_real.GetComponentInChildren<Text>().text = (j + 1).ToString();
                Key key_script = key_real.GetComponent<Key>();

                switch (i)
                {
                    case 0:
                        key_script.SetClip(whiteSounds1[j]);
                        break;
                    case 1:
                        key_script.SetClip(whiteSounds2[j]);
                        break;
                    case 2:
                        key_script.SetClip(whiteSounds3[j]);
                        break;
                    case 3:
                        key_script.SetClip(whiteSounds4[j]);
                        break;
                    case 4:
                        key_script.SetClip(whiteSounds5[j]);
                        break;
                    case 5:
                        key_script.SetClip(whiteSounds6[j]);
                        break;
                    case 6:
                        key_script.SetClip(whiteSounds7[j]);
                        break;
                    default:
                        break;
                }

                keys[i, j] = key_script;

                if (i != old_index)
                {
                    key_real.SetActive(false);
                }
            }
        }
    }
}

